package cl;

import base.BaseTest;
import cn.fangline.base.util.HeaderUtil;
import cn.fangline.wo.model.Agent;
import cn.fangline.wo.service.IAgentService;
import cn.fangline.wo.service.IOriginHouseService;
import cn.fangline.wo.service.IZoneService;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by 抽离 on 2017/5/2 0002.
 */
public class Anjuke extends BaseTest {

    ExecutorService executorService = Executors.newCachedThreadPool();

    @Autowired
    IZoneService zoneService;

    @Autowired
    private IAgentService agentService;

    @Autowired
    private IOriginHouseService originHouseService;

    private final static Logger logger = Logger.getLogger(Anjuke.class);

    public static final int EXECUTOR_MAX = 40;
    public static final int RETRY_COUNTER = 1000;//重试次数
    public static final int RETRY_COUNTER_REDUCE_STEP_CONNECT = 1;//重试递减次数
    public static final int RETRY_COUNTER_REDUCE_STEP_CONTAIN = 30;//抓出错误数据递减次数

    @Test
    public void test(){
//        try {
//            Document doc = Jsoup.connect("http://www.baidu.com")
//                    .proxy(new Proxy(Proxy.Type.HTTP
//                            , new InetSocketAddress("proxy.abuyun.com", 9010)))
//                    .header("Proxy-Authorization", "Basic SE80MDNUNzI3V1QwOThXUDozQzRBOTUxM0RCQ0EyQUZG")
//                    .header("Proxy-Switch-Ip", "yes")
//                    .get();
//            System.out.println("----------=" + doc);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        System.out.println("----------=" + "aa".substring(1));
    }

    @Test
    public  void testAnjukeHouse(){
        Agent agent = new Agent();
        agent.setAgentName("啦啦啦");
        agent.insert();
    }

    @Test
    public void testCrawlerAnjuke(){
        try {
            Document doc = Jsoup.connect("http://www.baidu.com/")
                    .userAgent(HeaderUtil.getUserAgent())
                    .proxy(new Proxy(Proxy.Type.HTTP
                            , new InetSocketAddress("proxy.abuyun.com", 9010)))
                    .header("Proxy-Authorization", "Basic SE80MDNUNzI3V1QwOThXUDozQzRBOTUxM0RCQ0EyQUZG")
                    .header("Proxy-Switch-Ip", "yes")
    //                                    , new InetSocketAddress("proxy.abuyun.com", 9020)))
    //                        .header("Proxy-Authorization", "Basic SDE1N1g0NTAxMDk4MkxFRDo5NjE3QzI4Njg1MzQ5NjM1")
                    .timeout(3000)
                    .get();
            System.out.println("----------=" + doc);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
